package org.javaboy.vhr.service;

import org.javaboy.vhr.mapper.AttendanceRecordMapper;
import org.javaboy.vhr.mapper.EmployeeMapper;
import org.javaboy.vhr.model.AttendanceRecord;
import org.javaboy.vhr.model.Employee;
import org.javaboy.vhr.service.impl.AttendanceRecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @create 2025-04-09 17:26
 */
@Service
public class AttendanceRecordService implements AttendanceRecordServiceImpl {

    @Autowired(required = false)
    private AttendanceRecordMapper attendanceRecordMapper;

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Override
    public List<AttendanceRecord> getAllAttendanceRecords() {

        return attendanceRecordMapper.getAllAttendanceRecords();
    }

    @Override
    public int insertAttendanceRecord(Integer eid, String clockType, String status,String remark) {
        // 参数校验
        if (eid == null || clockType == null || status == null) {
            throw new IllegalArgumentException("参数不能为空");
        }

        // 获取员工信息
        Employee employee = employeeMapper.getEmployeeById(eid);
        if (employee == null) {
            throw new IllegalArgumentException("员工不存在");
        }

        // 创建考勤记录对象
        AttendanceRecord record = new AttendanceRecord();
        record.setEid(eid);
        record.setClockTime(new Date());
        record.setClockType(clockType);
        record.setClockStatus(status);
        record.setRemark(remark);

        // 设置员工相关信息
        record.setEmployeeName(employee.getName());
        record.setPhone(employee.getPhone());
        record.setDepartmentName(employee.getDepartment().getName());

        // 插入记录
        return attendanceRecordMapper.insert(record);
    }

    @Override
    public List<AttendanceRecord> getAttendanceRecordsByEid(Integer eid) {
        if (eid == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (employeeMapper.getEmployeeById(eid) == null) {
            throw new IllegalArgumentException("员工不存在");
        }
        return attendanceRecordMapper.getAttendanceRecordsByEid(eid);
    }


    @Override
    public List<AttendanceRecord> getAttendanceRecordsByEname(String ename) {
        if (ename == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (employeeMapper.getEmployeeByName(ename) == null){
            throw new IllegalArgumentException("员工不存在");
        }
        return attendanceRecordMapper.getAttendanceRecordsByEname(ename);
    }

    @Override
    public int deleteAttendanceRecord(Integer id) {
        if (attendanceRecordMapper.getAttendanceRecordById(id)==null){

            throw new IllegalArgumentException("考勤记录不存在");
        }
        return attendanceRecordMapper.deleteAttendanceRecord(id);
    }

    @Override
    public int updateAttendanceRecord(AttendanceRecord attendanceRecord) {
        if (attendanceRecord.getId()==null){
            throw new IllegalArgumentException("考勤记录不存在");
        }

        return attendanceRecordMapper.updateAttendanceRecord(attendanceRecord);
    }


}
